<template>
  <el-dialog v-model="dialogFormVisible" @close="handleClose" :close-on-click-modal="false" :title="title"
             width="640px">
    <el-form
        v-loading="editLoading"
        ref="formRef"
        label-width="150px"
        :model="form"
        style="padding: 30px"
        :rules="rules"
    >
      <el-form-item label="名称：" required prop="title">
        <el-input v-model="form.title" autocomplete="off" maxlength="60" placeholder="请输入名称"/>
      </el-form-item>
      <el-form-item label="标签：" required prop="tag">
        <el-input v-model="form.tag" autocomplete="off" maxlength="60" placeholder="请输入标签"/>
      </el-form-item>
      <el-form-item label="说明：" required prop="note">
        <el-input v-model="form.note" autocomplete="off" maxlength="60" placeholder="请输入说明"/>
      </el-form-item>
      <el-form-item label="价格：" required prop="price">
        <el-input v-model="form.price" autocomplete="off" maxlength="60" placeholder="请输入价格"/>
      </el-form-item>
      <el-form-item label="折扣前价格：" required prop="realprice">
        <el-input v-model="form.realprice" autocomplete="off" maxlength="60" placeholder="请输入折扣前价格"/>
      </el-form-item>
      <el-form-item label="会员卡实际天数：" required prop="vipDays">
        <el-input v-model="form.vipDays" autocomplete="off" maxlength="60" placeholder="请输入会员卡实际天数"/>
      </el-form-item>
      <el-form-item label="会员卡类型：" required prop="vipType">
        <el-select v-model="form.vipType"   placeholder="请选择" clearable>
          <el-option
              v-for="item in [
                    { value: 1, label: '天卡' },
                    { value: 2, label: '周卡' },
                    { value: 3, label: '月卡' },
                    { value: 4, label: '季度' },
                    { value: 5, label: '年卡' },
                    { value: 6, label: 'SVIP' }
                  ]"
              :key="item.value"
              :label="item.label"
              :value="item.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="会员卡权益：" required prop="benefitsIdsArr">
        <el-select v-model="form.benefitsIdsArr" multiple   placeholder="请选择" clearable>
          <el-option
              v-for="item in benefitsIdOptions"
              :key="item.id"
              :label="item.title"
              :value="item.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="是否启用：" required prop="status">
        <el-radio-group v-model="form.status">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="0">否</el-radio>
        </el-radio-group>
      </el-form-item>

      <div class="dialog-footer" style="text-align: center">
        <el-button @click="handleClose" icon="remove">取消</el-button>
        <el-button type="primary" :loading="suLoading" @click="handleSubmit" icon="plus">{{
            form.id ? '更新' : '保存'
          }}
        </el-button>
      </div>
    </el-form>
  </el-dialog>
</template>
<script setup>
import KVA from '@/utils/kva.js'
import debounce from '@/utils/debounce'
import {getById,saveUpdate} from '@/api/users/vip.js'

import {getAllBenefits} from '@/api/users/benenfit.js'


const emits = defineEmits(["load"])
// 控制弹窗得显示
const dialogFormVisible = ref(false)
const authorityList = ref([])
const benefitsIdOptions = ref([])
// 这个数据模型
let form = ref({
  id:"",
  title:"",
  tag: "",
  note: "",
  price: "",
  realprice: '',
  vipDays: 1,
  vipType:1,
  benefitsIdsArr:[],
  benefitsIds:'',
  status: 1
})

// 弹出标题
const title = ref("添加")
const editLoading = ref(false)
const suLoading = ref(false)
// 表单ref, 用于提交最终得验证处理
const formRef = ref({})
// 表单验证
const rules = reactive({
  "title": [{required: true, message: '请输入名称', trigger: 'blur'}],
  "tag": [{required: true, message: '请输入标签', trigger: 'blur'}],
  "note": [{required: true, message: '请输入说明', trigger: 'blur'}],
  "price": [{required: true, message: '请输入价格', trigger: 'blur'}],
  "realprice": [{required: true, message: '请输入折扣前价格', trigger: 'blur'}],
  "vipDays": [{required: true, message: '请输入会员卡实际天数', trigger: 'blur'}],
  "vipType": [{required: true, message: '请选择会员卡类型', trigger: 'blur'}],
  "benefitsIdsArr": [{required: true, message: '请选择会员卡权益', trigger: 'blur'}],
  "status": [{required: true, message: '请选择是否启用', trigger: 'blur'}],
})


//获取所有权益数据
const getBenefitsIdOptions =  async () => {
  const resp = await getAllBenefits()
  benefitsIdOptions.value = resp.data
}

// 打开修改密码的弹窗
const handleOpen = async (opid,alist) => {
  getBenefitsIdOptions()
  authorityList.value = alist
  // 每次打开头重置
  dialogFormVisible.value = true;
  if (opid) {
    title.value = "编辑"
    editLoading.value = true;
    const resp = await getById(opid)
    editLoading.value = false;
    form.value = resp.data
    form.value.benefitsIdsArr = form.value.benefitsIds.split(',')
    title.value = "编辑【" + form.value.title + "】"
  }
}

// 保存和修改
const handleSubmit = () => {
  /*form.value.benefitsIds = form.value.benefitsIdsArr.join(',')
  console.log("form.value",form.value);
  return;*/

  suLoading.value = true
  debounce(() => {
    formRef.value.validate(async (valid, fields) => {
      if (valid) {
        try {
          form.value.benefitsIds = form.value.benefitsIdsArr.join(',')
          await saveUpdate(form.value)
          suLoading.value = false
          // 提示保存成功
          KVA.notifySuccess(form.value.id ? "更新成功" : "保存成功")
          // 关闭弹窗
          handleClose()
          // 刷新方法
          emits("load")
        } catch (ex) {
          suLoading.value = false
        }
      } else {
        suLoading.value = false
      }
    })
  }, 600)
}

const handleClose = () => {
  formRef.value.resetFields()
  suLoading.value = false
  editLoading.value = false
  dialogFormVisible.value = false
  form.value = {
    id:"",
    title:"",
    tag: "",
    note: "",
    price: "",
    realprice: 1,
    vipDays: 1,
    vipType:1,
    benefitsIdsArr:[],
    benefitsIds:'',
    benefitsIdsStr:'',
    status: 1
  }

}

// 把弹出打开得方法暴露给父组件进行调用
defineExpose({
  handleOpen
})
</script>
